<?php
$items = $this->contents;
$isSmartBlock = ($this->obj instanceof Application_Model_Block);
$isPlaylist = ($this->obj instanceof Application_Model_Playlist);
if ($isSmartBlock && $this->showPoolCount) { ?>
    <div class='sp_text_font sp_text_font_bold'>
        <span id='sp_pool_count' class='sp_text_font sp_text_font_bold'>
            <?php
            if($this->poolCount) {
                echo $this->poolCount;
                echo ngettext(" track matches your search criteria.", " tracks match your search criteria.", $this->poolCount);
            } else {
                echo "No track match the search criteria";
            }
            ?>
        </span>
        <?php if ($this->poolCount > 0) { ?>
            <span class='checked-icon sp-checked-icon' id='sp_pool_count_icon'></span>
        <?php } else { ?>
            <span class='sp-warning-icon' id='sp_pool_count_icon'></span>
        <?php } ?>
    </div>
<?php
}
if (count($items) && ($isSmartBlock || $isPlaylist)) : ?>
    <?php $i = 0; ?>
    <?php if ($isSmartBlock && !($this->obj->isStatic())) {
        echo _("</br>This is only a preview of possible content generated by the smart block based upon the above criteria.");
    }
    ?>
    <?php foreach ($items as $item) :
        $staticBlock = null;
        $fileUrl = null;
        $nextFileUrl = null;
        if ($item['type'] == 2) {
            $bl = new Application_Model_Block($item['item_id']);
            $staticBlock = $bl->isStatic();
            $fileUrl = null;
        } else if ($item['type'] == 1) {
            $fileUrl = null;
        } else if ($item['type'] == 0) {
            $audiofile = Application_Model_StoredFile::RecallById($item['item_id']);
            $fileUrl = $audiofile->getFileUrl();
        }

        if (($i < count($items) - 1) && ($items[$i + 1]['type'] == 0)) {
            $nextAudiofile = Application_Model_StoredFile::RecallById($items[$i + 1]['item_id']);
            $nextFileUrl = $nextAudiofile->getFileUrl();
        }
    ?>
        <li class="ui-state-default" id="spl_<?php echo $item["id"] ?>" unqid="<?php echo $item["id"]; ?>">
            <div class="list-item-container">

                <?php if ($item['type'] == 0 && $item['exists']) : ?>
                    <div class="big_play" audioFile="<?php echo $item["id"]; ?>" data-mime-type="<?php echo $item["mime"]; ?>">
                        <span class="ui-icon ui-icon-play"></span>
                    </div>
                <?php elseif ($item['type'] == 1 && $item['exists']) : ?>
                    <div class="big_play" data-mime-type="<?php echo $item["mime"]; ?>">
                        <span class="ui-icon ui-icon-play"></span>
                    </div>
                <?php elseif ($item['type'] == 2 && $item['exists']) : ?>
                    <div class="big_play" blockId="<?php echo $item["item_id"]; ?>" blocktype="<?php echo $staticBlock ? "static" : "dynamic" ?>">
                        <span class="ui-icon ui-icon-play"></span>
                    </div>
                <?php else : ?>
                    <div class="big_play ui-state-hover">
                        <span class="ui-icon ui-icon-alert"></span>
                    </div>
                <?php endif; ?>
                <div class="text-row top">
                    <span class="spl_playlength"><?php echo $item["length"] ?></span>
                    <?php if ($item['type'] == 0) { ?>
                        <!-- <span class="spl_cue ui-state-default"></span> -->
                        <?php } else if ($item['type'] == 2) {
                        if ($staticBlock) { ?>
                            <span class="spl_block_expand close" blockId="<?php echo $item["item_id"]; ?>" id="expand_block_<?php echo $item["id"] ?>">
                                <span class="ui-icon ui-icon-triangle-2-n-s"></span>
                                <?php echo _("Expand Static Block") ?>
                            </span>
                        <?php   } else { ?>
                            <span class="spl_block_expand close" blockId="<?php echo $item["item_id"]; ?>" id="expand_block_<?php echo $item["id"] ?>">
                                <span class="ui-icon ui-icon-triangle-2-n-s"></span>
                                <?php echo _("Expand Dynamic Block") ?>
                            </span>
                    <?php   }
                    } ?>
                    <span class="spl_title"><?php echo $item['track_title'] ?></span>
                </div>
                <div class="text-row">
                    <span class="spl_artist"><?php echo $item['creator'] ?></span>
                    <span class="spl_offset"><?php echo $item["offset"] ?></span>
                </div>
                <?php //create the crossfade icon.
                if (($i < count($items) - 1) && !($items[$i]['type'] == 2 && $items[$i + 1]['type'])) :
                ?>
                    <!-- <div id="fade_<?php echo $i ?>" class="spl_fade_control ui-state-default"></div> -->
                <?php endif; ?>
                <span id="remove_<?php echo $item["id"] ?>" class="ui-icon ui-icon-closethick"></span>
            </div>
            <?php if ($item['type'] == 0) { ?>
                <div id="cues_<?php echo $i ?>" class="cue-edit clearfix" style="display: none">
                    <?php echo $this->partial('playlist/set-cue.phtml', array(
                        'id' => $item["id"],
                        'cueIn' => $item['cuein'],
                        'cueOut' => $item['cueout'],
                        'cueInSec' => $item['cueInSec'],
                        'cueOutSec' => $item['cueOutSec'],
                        'uri' => $fileUrl,
                        'origLength' => $item['orig_length']
                    )); ?>
                </div>
            <?php } ?>


            <?php //create a fade editor box
            //(fadeout of current position + fade in of next position)
            if (($i < count($items) - 1) && !($items[$i]['type'] == 2 && $items[$i + 1]['type'])) :
            ?>
                <div id="crossfade_<?php echo $i ?>-<?php echo $i + 1 ?>" class="crossfade clearfix" style="display: none">
                    <?php

                    $vars = array(
                        'item1' => $items[$i]['id'],
                        'fadeOut' => $items[$i]['fadeout'],
                        'fadeIn' => $items[$i + 1]['fadein'],
                        'item1Type' => $items[$i]['type'],
                        'cueIn1' => $items[$i]['cueInSec'],
                        'cueOut1' => $items[$i]['cueOutSec'],
                        'item1Url' => $fileUrl
                    );

                    $item2 = array(
                        'item2Url' => $nextFileUrl,
                        'item2' => $items[$i + 1]['id'],
                        'item2Type' => $items[$i + 1]['type'],
                        'offset' => $items[$i]['trackSec'] - $items[$i + 1]['trackoffset'],
                        'cueIn2' => $items[$i + 1]['cueInSec'],
                        'cueOut2' => $items[$i + 1]['cueOutSec']
                    );

                    if (isset($nextFileUrl)) {
                        $vars = $vars + $item2;
                    }

                    echo $this->partial('playlist/set-fade.phtml', $vars);

                    ?>
                </div>
            <?php endif; ?>
            <?php if ($item['type'] == 2) { ?>
                <ul style='display:none' id="block_<?php echo $item["id"] ?>_info" class="smart-block-info expand-block-separate"></ul>
            <?php } ?>
        </li>
        <?php $i = $i + 1; ?>
    <?php endforeach; ?>

<?php else : ?>
    <div class="spl_empty">
        <?php
        if ($this->obj instanceof Application_Model_Block) {
            if ($this->obj->isStatic()) {
                echo _("Choose some search criteria above and click Generate to create this playlist.");
            } else {
                echo _("A track list will be generated when you schedule this smart block into a show.");
            }
        } else {
            echo _("Drag tracks here from your library to add them to the playlist");
        }
        ?>
    </div>
<?php endif; ?>
